<?php
/**
 * flash所需信息操作类
 * @author:lll
 * @since:2012 6.8
 * 
 */
class Flash_Info{
    
    const TABLE_LOAD_IMG = 'loadimg';
    
    const TABLE_ADD_IMG = 'addimg';
    const TABLE_ADD_LISTEN = 'addlisten';
    const TABLE_ADD_SP = 'addsp';
    const TABLE_ADD_TEXT = 'addtext';
    
    const TABLE_REMOVE_IMG = 'removeimg';
    const TABLE_REMOVE_LISTEN = 'removelisten';
    const TABLE_REMOVE_SP = 'removesp';
    const TABLE_REMOVE_TEXT = 'removetext';
    
    protected $uId;
    /**
     * db实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_dbInstance;
    /**
     * cache实例
     *
     * @var Helper_Cache_Memcache
     */
    protected $_cacheInstance;
    /**
     *单例对象
     */    
    public static $singletonObjects = array();
     /**
     *构造器
     * @param int $uId
     */
    protected function __construct( $uId )
    {
        $this->uId = $uId;
        $this->_dbInstance = $this->_getMysql();
        $this->_cacheInstance = $this->_getCache();
    }
    /**
     * 获取单例
     */
    public static function getInstance( $uId )
	{
		$className = __CLASS__;
		if( !isset( self::$singletonObjects[$uId][$className] ) )
		{
			self::$singletonObjects[$uId][$className] = new self( $uId );
		}

		return self::$singletonObjects[$uId][$className];
	}
        
     /*
     * 获取db实例
     */
    public function _getMysql() {

        return Common::getMysql();
    }
    
    /*
     * 获取cache实例
     */
    public function _getCache() {

        $mem = new Memcache();
        $mem->connect('127.0.0.1', '11211' );
        return $mem;
    }

    /**
     * 获取loadimg
     */
    public function getLoadImgList ( $classId , $lessonId , $testId ) {
        
        $loadImg = $this->_dbInstance->getList( self::TABLE_LOAD_IMG, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $loadImgList = array();
        foreach ($loadImg as $key => $value) {
            $loadImgList['img_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['imgId']] = array (
                'imgId' =>  'img_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['imgId'],
                'posX'  =>  $value['posX'],
                'posY'  =>  $value['posY'],
                'url'   =>  $value['imgUrl']
            );
        }
        
        return $loadImgList;
        
    }
    
    /**
     * 获取addimg
     */
    public function getAddImgList ( $classId , $lessonId , $testId ) {
        
        $addImg = $this->_dbInstance->getList(self::TABLE_ADD_IMG, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $addImgList = array();
        foreach ($addImg as $key => $value) {
            $addImgList['img_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['imgId']] = 'img_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['imgId'];
           
        }
        return $addImgList;
    }
    
    /**
     * 获取addlisten
     */
    public function getAddListenList ( $classId , $lessonId , $testId ) {
        
        $addListen = $this->_dbInstance->getList(self::TABLE_ADD_LISTEN, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $addListenList = array();
        foreach ($addListen as $key => $value) {
            $addListenList['listen_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['listenId']] = array (
                'listenId'  =>  'listen_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['listenId'],
                'type'      =>  $value['type'],
                'stepId'    =>  $value['nextStepId'],
                'targetId'  =>  $value['targetId']
            );
        }
        
        return $addListenList;
    }
    
    /**
     * 获取addsp
     */
    public function getAddSpList ( $classId , $lessonId , $testId ) {
        
        $addSp = $this->_dbInstance->getList(self::TABLE_ADD_SP, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $addSpList = array();
        foreach ($addSp as $key => $value) {
            $addSpList['sp_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['spId']] = array (
                'spId'  =>  'sp_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['spId'],
                'posX'      =>  $value['posX'],
                'posY'      =>  $value['posY'],
                'width'     =>  $value['width'],
                'height'    =>  $value['height']
            );
        }
        
        return $addSpList;
    }
    
    /**
     * 获取addtext
     */
    public function getAddTextList ( $classId , $lessonId , $testId ) {
        
        $addText = $this->_dbInstance->getList(self::TABLE_ADD_TEXT, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $addTextList = array();
        foreach ($addText as $key => $value) {
            $addTextList['text_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['textId']] = array (
                'textId'  =>  'text_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['textId'],
                'posX'      =>  $value['posX'],
                'posY'      =>  $value['posY'],
                'width'     =>  $value['width'],
                'height'    =>  $value['height'],
                'type'      =>  $value['type'],
                'restrict'     =>  $value['restrict'],
                'matchStr'    =>  $value['matchStr']
            );
        }
        
        return $addTextList;
    }
    
    /**
     * 获取removeimg
     */
    public function getRemoveImgList ( $classId , $lessonId , $testId ) {
        
        $removeImg = $this->_dbInstance->getList(self::TABLE_REMOVE_IMG, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $removeImgList = array();
        foreach ($removeImg as $key => $value) {
            
            $removeImgList['img_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['imgId']] = $value['targetId'];
           
        }
        return $removeImgList;
    }
    
    /**
     * 获取removelisten
     */
    public function getRemoveListenList ( $classId , $lessonId , $testId ) {
        
        $removeListen = $this->_dbInstance->getList(self::TABLE_REMOVE_LISTEN, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $removeListenList = array();
        foreach ($removeListen as $key => $value) {
            
            $removeListenList['listen_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['listenId']] = 
            array (
                'targetId'  =>  $value['targetId'],
                'type'  =>  $value['type']
                );
           
        }
        return $removeListenList;
    }
    
    /**
     * 获取removesp
     */
    public function getRemoveSpList ( $classId , $lessonId , $testId ) {
        
        $removeSp = $this->_dbInstance->getList(self::TABLE_REMOVE_SP, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $removeSpList = array();
        foreach ($removeSp as $key => $value) {
            
            $removeSpList['sp_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['spId']] = $value['targetId'];
           
        }
        return $removeSpList;
    }
    
    /**
     * 获取removetext
     */
    public function getRemoveTextList ( $classId , $lessonId , $testId ) {
        
        $removeText = $this->_dbInstance->getList(self::TABLE_REMOVE_TEXT, array( 'classId' => $classId , 'lessonId' => $lessonId, 'testId' => $testId ) );
        $removeTextList = array();
        foreach ($removeText as $key => $value) {
            
            $removeTextList['text_'.$value['classId'].':'.$value['lessonId'].':'.$value['testId'].':'.$value['stepId'].':'.$value['textId']] = $value['targetId'];
           
        }
        return $removeTextList;
    }
    
    /**
     * flash信息写入MC
     */
    
    public function setToCache ($classId, $lessonId, $testId) {
        
        $loadImgList = $this->getLoadImgList($classId, $lessonId, $testId);
        
        $addImgList = $this->getAddImgList($classId, $lessonId, $testId);
        $addListenList = $this->getAddListenList($classId, $lessonId, $testId);
        $addSpList = $this->getAddSpList($classId, $lessonId, $testId);
        $addTextList = $this->getAddTextList($classId, $lessonId, $testId);
        
        $removeImgList = $this->getRemoveImgList($classId, $lessonId, $testId);
        $removeListenList = $this->getRemoveListenList($classId, $lessonId, $testId);
        $removeSpList = $this->getRemoveSpList($classId, $lessonId, $testId);
        $removeTextList = $this->getRemoveTextList($classId, $lessonId, $testId);
        
        $flashList  =   array (
            $classId   =>  array(
                $lessonId   =>  array (
                    $testId =>  array (
                        'loadImgList'       =>  $loadImgList,
                        'addImgList'        =>  $addImgList,
                        'addListenList'     =>  $addListenList,
                        'addSpList'         =>  $addSpList,
                        'addTextList'       =>  $addTextList,
                        'removeImgList'     =>  $removeImgList,
                        'removeListenList'  =>  $removeListenList,
                        'removeSpList'      =>  $removeSpList,
                        'removeTextList'    =>  $removeTextList,
                    )
                )
            )
        );
        
        $this->_cacheInstance->set($this->uId,$flashList);
        
    }
    
    /**
     * 获取cache
     */
    
    public function getCache () {

        return $this->_cacheInstance->get($this->uId);
    }
    
}